﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ucntemaxp.DB;
using System.Linq.Expressions;
using System.Windows.Forms;

namespace ucntemaxp.Ctr
{
    class CtrBooking
    {
        private DB.Dm74_1Entities DBCon;

        public CtrBooking()
        {
            DBCon = new DB.Dm74_1Entities();
        }

        public void deleteBooking(Booking b)
        {
            try
            {
                var sæder = from x in DBCon.Sæde where x.Booking.Bookingid == b.Bookingid select x;
                foreach (Sæde s in sæder)
                {
                    s.Booking = null;
                    s.Sædestatus = "fri";
                }

                Booking boo = DBCon.Bookings.First(c => c.Bookingid == b.Bookingid);
                DBCon.Bookings.DeleteObject(boo);
                DBCon.SaveChanges();

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public List<Booking> findBookingsPåKunde(string tlfnr)
        {
            int tlf = Convert.ToInt32(tlfnr);
            try
            {
                var bookings = from x in DBCon.Bookings where x.Kunde.Tlf == tlf select x;
                List<Booking> bookList = new List<Booking>();
                foreach(var book in bookings)
                {
                    bookList.Add(book);
                }
                return bookList;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public void addBooking(string status, int sæder, int? kid, int fid)
        {
            try
            {
                DBCon.Bookings.AddObject(new Booking()
                {
                    Status = status,
                    Sæder = sæder,
                    Kid = kid,
                    Fid = fid
                });
                

                DBCon.SaveChanges();
                
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void updateSæde(List<int> sædebooks, string status, int fid)
        {
            var booking = (from x in DBCon.Bookings select x.Bookingid).Max();
            
            foreach(int sæ in sædebooks)
            {
               try 
               {
                    Sæde s = DBCon.Sæde.First(i => i.Plads == sæ && i.Forestillingid==fid);
                    s.Sædestatus = "Optaget";
                    s.Booking = DBCon.Bookings.First(i => i.Bookingid == booking);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            DBCon.SaveChanges();
        }

        public void removeBookings(int bookingid)
        {
            DBCon.Bookings.DeleteObject(new Booking()
            {
                Bookingid = bookingid
            });
        }

        public void updateBooking(int bookingid, string status, int sæder, int kid, int fid)
        {
            try
            {
                Booking b = DBCon.Bookings.First(i => i.Bookingid == bookingid);
                b.Status = status;
                b.Sæder = sæder;
                b.Kid = kid;
                b.Fid = fid;
                DBCon.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(Action<Booking> updateStatement, Expression<Func<Booking, bool>> where)
        {
            // get your object context & objectset, cast to IQueryable<T>
            var table = (IQueryable<Booking>)DBCon.CreateObjectSet<Booking>();

            // filter with the Expression
            var items = table.Where(where);

            // perform the Action on each item
            foreach (var item in items)
            {
                updateStatement(item);
            }

            // save changes.
        }
    }
}
